var MobileBFG = MobileTrapeze.extend({
    
    conf                : null,
    $chrome             : null,
    geocoder            : null,
    geolocation         : null,
    
    touch_y_start       : 0,
    touch_y_delta       : 0,
    touch_y_minimum     : 5,
    
    request_location    : false,
    
    get_geolocation : function() {
        if (navigator.geolocation) {  /* geolocation is available */
            navigator.geolocation.getCurrentPosition(
                this.success_geolocation.bind(this),
                this.error_geolocation.bind(this),
                { enableHighAccuracy: false, maximumAge: 5000, timeout: 5000 }
            );
        }
    },
    
    success_geolocation : function(response) {
        if (!response.error) {
            $('html').addClass('has-geolocation');
            
            this.geolocation = {
                lat : typeof(response.latitude) != 'undefined' ? response.latitude : response.coords.latitude,
                lng : typeof(response.longitude) != 'undefined' ? response.longitude : response.coords.longitude
            };
            
            document.getElementById('id_location').placeholder = 'Current Location';
            document.getElementById('id_lat').value = this.geolocation.lat;
            document.getElementById('id_lng').value = this.geolocation.lng;
            
            if (this.request_location) {
                $.getJSON(this.conf.locator+'?lat='+this.geolocation.lat+'&lng='+this.geolocation.lng, this.save_location.bind(this));
            }
        }
    },
    
    error_geolocation : function(response) {
        $('#Favorite').removeClass('loading');
    },
    
    save_location : function(json) {
        $('#Favorite .location').html('BFG '+json.location_name);
        $('#Favorite .details').attr('href', json.location_url);
        $('#Favorite').removeClass('loading');
        this.create_cookie(json.cookie_name, json.location_id);
        this.create_cookie(this.conf.cookie_set_by, '1', 2);
    },
    
    dropdown_locator : function() {
        this.$chrome.removeClass(this.conf.class_search).toggleClass(this.conf.class_locator);
    },
    
    dropdown_search : function() {
        if (this.$chrome.removeClass(this.conf.class_locator).toggleClass(this.conf.class_search)) {
            document.getElementById('id_q').focus();
        }
    },
    
    dropdown_touch : function(evt) {
        switch(evt.type) {
            case 'touchstart':
                this.touch_y_start = evt.touches[0].pageY;
                this.touch_y_delta = 0;
                break;
            
            case 'touchmove':
                this.touch_y_delta = evt.touches[0].pageY - this.touch_y_start;
                break;
            
            case 'touchend':
                if (this.touch_y_delta > this.touch_y_minimum || this.touch_y_delta < -this.touch_y_minimum) {
                    this.$chrome.removeClass(this.conf.class_search).removeClass(this.conf.class_locator);
                }
                break;
            
            default: return;
        }
    },
    
    submit_locator : function() {
        document.getElementById('id_location').value = '';
        document.getElementById('id_geocode_results').value = '';
        document.getElementById('FormLocator').submit();
    },
    
    init : function(options) {
        var defaults = {
            locator             : null,
            class_locator       : 'dropdown-locator',
            class_search        : 'dropdown-search',
            class_favorite_load : 'loading',
            cookie_set_by       : 'favorite_location_set_by_gps'
        }
        this.conf = this.extend(defaults, options);
        
        this.$chrome = $('#Chrome');
        this.geocoder = new google.maps.Geocoder();
        
        this.get_geolocation();
        
        // Flag to submit geodata for closest location -- limit to once per 2 hours
        if (this.conf.locator && !this.read_cookie(this.conf.cookie_set_by)) {
            this.request_location = true;
            $('#Favorite').addClass(this.conf.class_favorite_load);
        }
        
        // Init Locator
        $('#NavLocator a').bind('click', (function(evt) {
            evt.preventDefault();
            this.dropdown_locator();
        }).bind(this));
        
        
        $('#LocatorClear').bind('click', (function(evt) {
            evt.preventDefault();
            this.submit_locator();
        }).bind(this));
        
        
        $('#LocatorSearch').bind('click', (function(evt) {
            evt.preventDefault();
            var location = document.getElementById('id_location').value;
            if (location.length > 0) {
                this.geocoder.geocode({ 'address': location}, function(response, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        var results = new Array(), i = 0, places = response.length;
                        for ( i; i < places; i++) {
                            results[i] = [response[i].formatted_address, [response[i].geometry.location.lat(), response[i].geometry.location.lng()]];
                        }
                    }
                    document.getElementById('id_geocode_results').value = JSON.stringify(results);
                    document.getElementById('FormLocator').submit();
                });
            } else { // clear and submit
                document.getElementById('id_geocode_results').value = '';
                document.getElementById('FormLocator').submit();
            }
        }).bind(this));
        
        // Init Search
        $('#NavSearch a').bind('click', (function(evt) {
            evt.preventDefault();
            this.dropdown_search();
        }).bind(this));
        
        if ($('body').attr('class')) {
            var page_classes = $('body').attr('class').match(/\S+-page/g);
            if (page_classes != null) {
                var num_page_classes = page_classes.length;
                
                for (var i = 0; i < num_page_classes; i++){ 
                    switch(page_classes[i]) {
                        
                        case 'blog-page':
                            $('.blog-title').bind('click', (function(evt) {
                                evt.preventDefault();
                                $(evt.target).closest('article').toggleClass('expand');
                            }).bind(this));
                            break;
                            
                        case 'menu-details-page':
                            $('.menu-locator').bind('click', (function(evt) {
                                evt.preventDefault();
                                this.submit_locator();
                            }).bind(this));
                            break;
                            
                        default:
                            $('a[href="/locator/"]').each((function(item, i) {
                                $(item).bind('click', (function(evt) {
                                    evt.preventDefault();
                                    this.submit_locator();
                                }).bind(this));
                            }).bind(this));
                            break;
                        
                    }
                }
            }
        }
        
        document.addEventListener('touchstart', this.dropdown_touch.bind(this));
        document.addEventListener('touchmove', this.dropdown_touch.bind(this));
        document.addEventListener('touchend', this.dropdown_touch.bind(this));
        
        //this.create_cookie('force_site', '', 0.006); // TODOF Remove, this is for debugging only : will clear the force desktop cookie
    }
});